package de.cbf.cam.dsa.services.impl.rcp_provider.act;

import java.util.Hashtable;

import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

import de.cbf.cam.dsa.services.impl.rcp_provider.extension.RCPServiceImplExtensionPointResovler;
import de.cbf.cam.dsa.services.impl.rcp_provider.factorys.ServiceFactoryPersister;
import de.cbf.cam.dsa.services.impl.rcp_provider.factorys.ServiceFactoryProviderCultureDSA;
import de.cbf.cam.dsa.services.impl.rcp_provider.factorys.ServiceFactoryProviderMonthsDSA;
import de.cbf.cam.dsa.services.impl.rcp_provider.factorys.ServiceFactoryProviderProfessionDSA;
import de.cbf.cam.dsa.services.impl.rcp_provider.factorys.ServiceFactoryProviderRCPStructureDSA;
import de.cbf.cam.dsa.services.impl.rcp_provider.factorys.ServiceFactoryProviderRaceDSA;
import de.cbf.cam.dsa.services.impl.rcp_provider.factorys.ServiceFactoryRCPFinder;
import de.cbf.cam.dsa.services.impl.rcp_provider.persister_service.IServiceRCPPersister;
import de.cbf.cam.dsa.services.interfaces.rcp_provider.IServiceDSAProviderCulture;
import de.cbf.cam.dsa.services.interfaces.rcp_provider.IServiceDSAProviderMonths;
import de.cbf.cam.dsa.services.interfaces.rcp_provider.IServiceDSAProviderProfession;
import de.cbf.cam.dsa.services.interfaces.rcp_provider.IServiceDSAProviderRCPStructure;
import de.cbf.cam.dsa.services.interfaces.rcp_provider.IServiceDSAProviderRace;
import de.cbf.cam.dsa.services.interfaces.rcp_provider.IServiceDSARCPFinder;

/**
 * The activator class controls the plug-in life cycle
 * TODO add preferences to choose provider(s)
 */
public class ActivatorRCPServiceImpl extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "de.cbf.cam.dsa.services.impl.rcp_provider"; //$NON-NLS-1$

	// The shared instance
	private static ActivatorRCPServiceImpl plugin;

	/**
	 * The constructor
	 */
	public ActivatorRCPServiceImpl() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;

		//make special port for extension points
		RCPServiceImplExtensionPointResovler.getInstance().setBundleContext(context);

		registerServices(context);
	}

	protected void registerServices(BundleContext context) {
		registerProviderRaceService(context);
		registerProviderCultureService(context);
		registerProviderProfessionService(context);
		registerProviderRCPStructureService(context);
		registerProviderMonthsService(context);
		registerRCPFinderService(context);
		registerRCPPersisterService(context);
	}



	protected void registerProviderRaceService(BundleContext context) {
		context.registerService(
				IServiceDSAProviderRace.class.getName(),
				new ServiceFactoryProviderRaceDSA(),
				new Hashtable<String,Object>());
	}

	protected void registerProviderCultureService(BundleContext context) {
		context.registerService(
				IServiceDSAProviderCulture.class.getName(),
				new ServiceFactoryProviderCultureDSA(),
				new Hashtable<String,Object>());
	}

	protected void registerProviderProfessionService(BundleContext context) {
		context.registerService(
				IServiceDSAProviderProfession.class.getName(),
				new ServiceFactoryProviderProfessionDSA(),
				new Hashtable<String,Object>());
	}

	protected void registerProviderRCPStructureService(BundleContext context) {
		context.registerService(
				IServiceDSAProviderRCPStructure.class.getName(),
				new ServiceFactoryProviderRCPStructureDSA(),
				new Hashtable<String,Object>());
	}

	protected void registerProviderMonthsService(BundleContext context) {
		context.registerService(
				IServiceDSAProviderMonths.class.getName(),
				new ServiceFactoryProviderMonthsDSA(),
				new Hashtable<String,Object>());
	}

	protected void registerRCPPersisterService(BundleContext context) {
		context.registerService(
				IServiceRCPPersister.class.getName(),
				new ServiceFactoryPersister(),
				new Hashtable<String,Object>());
	}

	protected void registerRCPFinderService(BundleContext context) {
		context.registerService(
				IServiceDSARCPFinder.class.getName(),
				new ServiceFactoryRCPFinder(),
				new Hashtable<String,Object>());
	}
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static ActivatorRCPServiceImpl getDefault() {
		return plugin;
	}

}
